package multi.data;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.provider.MediaStore.Images.Thumbnails;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAssist extends ArrayAdapter<String> {
	private TextView tvLV;
	private ImageView ivLV;
	private List<String> data = new ArrayList<String>();
	private Bitmap thumbnail = null;

	public ListViewAssist(Context context, int textViewResourceId,
			List<String> objects) {
		super(context, textViewResourceId, objects);
		this.data = objects;
	}

	public int getCount() {
		return this.data.size();
	}

	public String getItem(int index) {
		return this.data.get(index);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View row;

		if (null == convertView) {
			LayoutInflater inflater = (LayoutInflater) this.getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			row = inflater.inflate(R.layout.listview, null);
		} else {
			row = convertView;
		}

		String item = getItem(position).toString();

		tvLV = (TextView) row.findViewById(R.id.textViewLV);
		ivLV = (ImageView) row.findViewById(R.id.imageViewLV);

		if (item.equals("/")) {
			ivLV.setImageResource(R.drawable.root);
		} else if (item.equals("../")) {
			ivLV.setImageResource(R.drawable.up);
		} else if (item.substring(item.length() - 1, item.length()).equals("/")) {
			ivLV.setImageResource(R.drawable.folder);
		} else if (item.substring(item.length() - 3, item.length()).equals(
				"mp4")) {
			thumbnail = ThumbnailUtils.createVideoThumbnail(item,
					Thumbnails.MICRO_KIND);
			ivLV.setImageBitmap(thumbnail);
		} else if (item.substring(item.length() - 3, item.length()).equals(
				"jpg")) {
			BitmapFactory.Options options = new BitmapFactory.Options();
			options.inSampleSize = 16;
			Bitmap myBitmap = BitmapFactory.decodeFile(item, options);
			ivLV.setImageBitmap(myBitmap);
		} else {
			ivLV.setImageBitmap(null);
		}

		tvLV.setText(item);
		return row;
	}

}
